package gestionedt.models;

import gestionedt.controllers.PrestationJpaController;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 *
 */
@Entity
@Table(name = "enseignant")
@NamedQueries({@NamedQuery(name = "Enseignant.findAll",
                           query = "SELECT e FROM Enseignant e"),
               @NamedQuery(name = "Enseignant.findById",
                           query = "SELECT e FROM Enseignant e WHERE e.id = :id"),
               @NamedQuery(name = "Enseignant.findByNom",
                           query = "SELECT e FROM Enseignant e WHERE e.nom = :nom"),
               @NamedQuery(name = "Enseignant.findByPrenom",
                           query = "SELECT e FROM Enseignant e WHERE e.prenom = :prenom")})
public class Enseignant implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "nom")
    private String nom;
    @Column(name = "prenom")
    private String prenom;
    @JoinTable(name = "indisponibilite_enseignant", joinColumns = {@JoinColumn(name = "idEnseignant", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "numeroCreneau", referencedColumnName = "numero")})
    @ManyToMany
    private Collection<Creneau> creneauCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEnseignant")
    private Collection<Prestation> prestationCollection;

    public Enseignant() {
    }

    public Enseignant(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public Collection<Creneau> getIndisponibilites() {
        return creneauCollection;
    }

    public void setIndisponibilites(Collection<Creneau> creneauCollection) {
        this.creneauCollection = creneauCollection;
    }

    public boolean estIndispo(Creneau creneau) {
        return creneauCollection.contains(creneau);
    }

    public boolean estDispo(Creneau creneau) {
        return !estIndispo(creneau);
    }

    public Collection<Prestation> getPrestationsAssurees() {
        return prestationCollection;
    }

    public void setPrestationsAssurees(Collection<Prestation> prestationCollection) {
        this.prestationCollection = prestationCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Enseignant)) {
            return false;
        }
        Enseignant other = (Enseignant) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getIdentite();
    }

    public String getIdentite() {
        return nom + " " + prenom;
    }

    /**
     * Indique si l'enseignant assure une prestation ou non
     * durant le créneau passé en paramètre.
     * @param creneau
     * @return
     */
    public boolean assurePrestationLe(Creneau creneau) {
        PrestationJpaController ctPrest = new PrestationJpaController();
        return ctPrest.assurePrestation(this, creneau);
    }

}
